﻿// Test_UDXService.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include "pch.h"
#include <tchar.h>
#include <list>
#include <WinSock2.h>
#include <Windows.h>
using namespace std;
#include "../../../../NetEngine_Windows/NetEngine_SourceCode/XyRyNet_CommHdr.h"
#include "../../../../NetEngine_Windows/NetEngine_SourceCode/XyRyNet_ProtocolHdr.h"
#include "../../../../NetEngine_Windows/NetEngine_SourceCode/NetEngine/NetEngine_Core/NetCore_Define.h"
#include "../../../../NetEngine_Windows/NetEngine_SourceCode/NetEngine/NetEngine_Core/NetCore_Error.h"
#pragma comment(lib,"../../../../NetEngine_Windows/NetEngine_SourceCode/Debug/NetEngine_Core.lib")
#pragma comment(lib,"Ws2_32.lib")

TCHAR tszClientAddr[32];

//客户进入
void __stdcall NetCore_UDX_CBLogin(LPCSTR lpszClientAddr, LPVOID lParam)
{
	printf("有用户连接:%s\n", lpszClientAddr);
	_tcscpy_s(tszClientAddr, sizeof(tszClientAddr), lpszClientAddr);
}
//客户离开
void __stdcall NetCore_UDX_CBLeave(LPCSTR lpszClientAddr, LPVOID lParam)
{
	printf("有用户离开:%s\n", lpszClientAddr);
}

int main()
{
	WSADATA st_WSAData;
	WSAStartup(MAKEWORD(2, 2), &st_WSAData);

	XNETHANDLE xhNet;
	NETCORE_UDXCONFIG st_UDXConfig;
	LPCTSTR lpszFile = _T("F:\\netengineapp\\NetEngine_WINApps\\Debug\\2.png");
	FILE *pSt_File = _tfopen(lpszFile, "wb");
	if (NULL == pSt_File)
	{
		printf(_T("创建文件失败 %d"),GetLastError());
		return 0;
	}

	st_UDXConfig.bEnableLogin = TRUE;
	st_UDXConfig.bEnableReorder = TRUE;
	st_UDXConfig.bEnableRryTime = TRUE;
	st_UDXConfig.nWindowSize = 1000000;

	memset(tszClientAddr, '\0', sizeof(tszClientAddr));

	if (!NetCore_UDXSocket_InitEx(&xhNet, &st_UDXConfig, 1339))
	{
		printf("启动UDX服务失败");
		return -1;
	}
	NetCore_UDXSocket_CBSetEx(xhNet, NetCore_UDX_CBLogin, NetCore_UDX_CBLeave);

	int i = 0;
	while (1)
	{
		int nMsgLen = 2048;
		TCHAR tszMsgBuffer[2048];
		memset(tszMsgBuffer, '\0', sizeof(tszMsgBuffer));
		if (NetCore_UDXSocket_RecvEx(xhNet, tszClientAddr, tszMsgBuffer, &nMsgLen))
		{
			fwrite(tszMsgBuffer, 1, nMsgLen, pSt_File);
			//NetCore_UDXSocket_SendEx(xhNet, tszClientAddr, "abc", 3);
			//printf("%d=%d\n", i, nMsgLen);
		}
		
		i++;
		Sleep(1);
	}

	WSACleanup();
	return 0;
}